import { Component, OnInit } from '@angular/core';
import {ExtendHttpService} from "../../../common/extend-http.service";
import {TipService} from "../../services/tip.service";
import {ActivatedRoute, Router} from "@angular/router";

@Component({
  selector: 'app-table-reserve',
  templateUrl: './table-reserve.component.html',
  styleUrls: ['./table-reserve.component.css']
})
export class TableReserveComponent implements OnInit {
  loading = true;
  tipType = 'error';
  tid = '';
  state:number;
  infoList = [];
  isFlag = false;
  url = '/main/table/table-list';
  constructor(
    private extendHttp: ExtendHttpService,
    private tipService: TipService,
    private router: Router,
    private activatedRoute: ActivatedRoute

  ) {
    this.activatedRoute.queryParams.subscribe(params => {
      this.tid = params['of.tid'];
      this.state = params['state'];
    });
  }

  back() {
    if (this.state == 1) {
      this.router.navigate(['/main/order/order-submit'], {queryParams: {'of.tid': this.tid, tableState: 2}});
    } else {
      this.router.navigate([this.url]);
    }


  }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }
    this.extendHttp.post('/merchantApp/singleprocess/findBookByTid',  {tid: this.tid}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.infoList = data.bookList;
        this.isFlag = true;
        this.loading = false;
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(()=> {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

}
